package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 其他参数
 * @author stone
 * @date 2021/12/31 15:22
 */
@RestController
@RequestMapping("other")
public class OtherController {

    //localhost:8080/other/hello?username=songge
    @RequestMapping("hello")
    public BaseRespVo hello(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        return BaseRespVo.ok(username);
    }

    @RequestMapping("cookie")
    public BaseRespVo cookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println("cookie:" + cookie.getName() + " → " + cookie.getValue());
        }
        return BaseRespVo.ok();
    }

    //localhost:8080/other/session/put?username=songge
    @RequestMapping("session/put")
    public BaseRespVo put(HttpServletRequest request,String username) {
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        return BaseRespVo.ok();
    }

    //localhost:8080/other/session/get
    @RequestMapping("session/get")
    public BaseRespVo get(HttpSession session) {
        Object username = session.getAttribute("username");
        return BaseRespVo.ok(username);
    }
}
